package com.unknowpupil.sell.converter;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.unknowpupil.sell.common.enums.ErrorCodeEnum;
import com.unknowpupil.sell.domain.OrderDetail;
import com.unknowpupil.sell.dto.OrderDTO;
import com.unknowpupil.sell.exception.SellException;
import com.unknowpupil.sell.form.OrderForm;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @Classname: OrderForm2OrderDTO
 * @Author: <a> w10g </a>
 * @CreateDate: Create in 2020/1/1 15:07
 * @Description:
 * @Version: 1.0
 */
@Slf4j
public class OrderForm2OrderDTO {

    public static OrderDTO convert (OrderForm orderForm){
        Gson gson = new Gson();
        OrderDTO orderDTO = new OrderDTO();
        orderDTO.setBuyerName(orderForm.getName());
        orderDTO.setBuyerPhone(orderForm.getPhone());
        orderDTO.setBuyerAddress(orderForm.getAddress());
        orderDTO.setBuyerOpenid(orderForm.getOpenId());
        List<OrderDetail> orderDetailList;
        try{
            orderDetailList = gson.fromJson(orderForm.getItems(),new TypeToken<List<OrderDetail>>(){}.getType());

        }catch (Exception e){
           log.error("【转换出错】json{}",orderForm.getItems());
            throw  new SellException(ErrorCodeEnum.PARAM_ERROR);
        }
        orderDTO.setOrderDetailList(orderDetailList);
        return orderDTO;
    }

/**
 *  name:"张三"
 *  phone:"18723981839"
 *  address:"dfjla"
 *  openid:"dfadf"
 *  items:[{
 *      productid:"2312312",
 *      productQuantity:2
 *  }]
 */
}
